﻿using EmoneyWeiXin.Filter;
using EmoneyWeiXinBLL;
using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EmoneyWeiXin.Controllers
{
    public class ZTTZController : Controller
    {


        string url = "http://api.themestock.emoney.cn/api/Home/GetAllTheme";

        string url2 = "http://api.themestock.emoney.cn/api/Home/GetTheme?id={0}";

        string url3 = "http://api.themestock.emoney.cn/api/Home/GetThemeStock?code={0}"; //根据主题获取股票

        string url4 = "http://api.themestock.emoney.cn/api/Home/GetFlashJson?code={0}&sdate={1}";

        MemcachedTools1 mmtoos = new MemcachedTools1();
        //
        // GET: /ZTTZ/

        //获取微信服务器ip
        public ActionResult GetIps()
        {

              string url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token={0}";
              url = string.Format(url, TokenHelper.GetToken("wxd14f20f92318c816", "fd283753536bdc361f9ce021341ad00f"));
              string result = NetHelper.LoadData(url, null);
              string json = NetHelper.LoadData(url, null);
              return View();
        }

        public ActionResult Index()
        {

            try
            {
                string json = WebApiHelper.CallGetApi(url);
                ReturnData returnData = JsonTools.ToObject<ReturnData>(json);
                List<ThemeEntity> themeNewList = new List<ThemeEntity>();// 最新主题
                List<ThemeEntity> themeHotList = new List<ThemeEntity>();//最热主题

                if (returnData != null && returnData.RetCode == "0" && returnData.Message != null)
                {
                    List<ThemeEntity> ThemeList = JsonTools.ToObject<List<ThemeEntity>>(returnData.Message.ToString());

                    if (ThemeList == null)
                    {
                        ThemeList = new List<ThemeEntity>();

                    }
                    foreach (var item in ThemeList) //循环去获取每个主题下的股票
                    {
                        List<Theme_Stock> themeStockList = GetStockLst(item.Theme_Code);
                        if (themeStockList != null && themeStockList.Count > 0)
                        {
                            Theme_Stock firstStock = themeStockList.OrderByDescending(n => n.ChangeRatio).ToList()[0];
                            item.FirstStock = firstStock;
                        }
                    }
                    int totalCount = ThemeList.Count;
                    themeNewList = ThemeList.OrderByDescending(n => n.Created_date).ToList();
                    //themeHotList = ThemeList.OrderByDescending(n => n.Created_date).Take(8).ToList();

                    #region  获取最热主题

                    string key = StringTools.ChangeRealKey("codeHots", "123");
                    List<string> codeHotsLst = mmtoos.Get<List<string>>(key);

                    if (codeHotsLst == null)
                    {
                        codeHotsLst = new List<string>();
                    }
                    foreach (var hotCode in codeHotsLst)
                    {
                        var ser = ThemeList.FirstOrDefault(n => n.Theme_Code == hotCode);
                        if (ser != null && themeHotList.Count < totalCount)
                        {
                            themeHotList.Add(ser);
                        }
                    }
                    if (themeHotList.Count < totalCount)
                    {
                        foreach (var item in themeNewList)
                        {
                            if (themeHotList.Count < totalCount && !themeHotList.Contains(item))
                            {
                                themeHotList.Add(item);
                            }
                        }
                    }
                    #endregion

                    ViewBag.ThemeHotList = themeHotList;
                    ViewBag.ThemeNewList = themeNewList;
                }
                return View();
            }
            catch (Exception)
            {

                return Content("页面发生了错误请刷新后重试");
            }

        }

        private List<Theme_Stock> GetStockLst(string themeCode)
        {
            List<Theme_Stock> result = null;
            string url = string.Format(url3, themeCode);
            string json = WebApiHelper.CallGetApi(url);
            ReturnData returnData = JsonTools.ToObject<ReturnData>(json);
            if (returnData != null && returnData.RetCode == "0" && returnData.Message != null)
            {
                result = JsonTools.ToObject<List<Theme_Stock>>(returnData.Message.ToString());

            }
            return result;
        }


        public ActionResult Detail()
        {
            string themeId = Request["themeId"];

            if (themeId != null)
            {
                url2 = string.Format(url2, themeId);
                string json = WebApiHelper.CallGetApi(url2);
                ReturnData returnData = JsonTools.ToObject<ReturnData>(json);
                if (returnData != null && returnData.RetCode == "0" && returnData.Message != null)
                {
                    ThemeEntity entity = JsonTools.ToObject<ThemeEntity>(returnData.Message.ToString());
                    ZTHotsRecordBLL.AddOneRecord(entity.Theme_Code);

                    //获取主题指数历史数据
                    url4 = string.Format(url4, entity.Theme_Code, System.DateTime.Now.AddDays(-10));
                    json = WebApiHelper.CallGetApi(url4);
                    returnData = JsonTools.ToObject<ReturnData>(json);
                    if (returnData != null && returnData.RetCode == "0" && returnData.Message != null)
                    {
                        List<Theme_Index> list = JsonTools.ToObject<List<Theme_Index>>(returnData.Message.ToString());
                        string themeIndexs = string.Empty;
                        List<string> themeDt = new List<string>();
                        var ser = list.OrderByDescending(n => n.Tradedate).Take(6).OrderBy(n => n.Tradedate).ToList();
                        for (int i = 0; i < ser.Count - 1; i++)
                        {
                            themeIndexs += ser[i].ClosePrice + ",";
                            themeDt.Add(ser[i].Tradedate.ToString("MM-dd"));
                        }
                        themeDt.Add(ser[ser.Count - 1].Tradedate.ToString("MM-dd"));
                        entity.HistoryZS = themeIndexs + (entity.ClosePrice_Now + (entity.ClosePrice_Now * (entity.ChangeRatio / 100))).ToString("0.000");
                        entity.HistoryDt = themeDt;
                    }

                    ViewBag.CurrentTheme = entity;
                    List<Theme_Stock> stockList = GetStockLst(entity.Theme_Code);
                    ViewBag.StockList = stockList;
                }

            }
            return View();
        }
        //public

    }
}
